package com.jiang.rest;

import java.util.Date;

import javax.servlet.ServletContext;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.Request;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import com.jiang.vo.Emp;

@Path("/info") // 这里不能使用/*做多业务处理，老老实实每个写
public class HelloJersey {
	
	@Context
	private Request request;
	@Context
	private ServletContext context;
	
	@GET
	@Produces("text/plain;charset=UTF-8")
	public String info() {
		return "你好啊，蒋正午";
	}
	
	@GET
	@Path("/getEmp")
	public String getEmp() {
		Emp emp = new Emp();
		emp.setEmpNo("2203");
		emp.setSalary(11.21);
		emp.setBirthday(new Date());
		JSONObject json = new JSONObject();
		try {
			json.put("emp", emp);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return json.toString();
	}
	
	@GET
	@Path("/getInner")
	@Produces("text/plain;charset=UTF-8")
	public String getInnerObj(@QueryParam("id") String id) {
		
		System.out.println(request.getMethod());
		System.out.println(context.getRealPath("/"));
		System.out.println(id);
		return "hello 蒋正午";
	}
	
	@POST  //表单形式提交这种只能用POST,如果使用GET就要像上面那么传参@QueryParam
	@Path("/getFormParam")
	@Produces("text/plain;charset=UTF-8")
	public String getFormParam(@FormParam("empNo") String id) {
		
		System.out.println(request.getMethod());
		System.out.println(context.getRealPath("/"));
		System.out.println(id);
		return "hello 蒋正午";
	}
	
	@GET  
	@Path("/getPathParam/{name}")
	@Produces("text/plain;charset=UTF-8")
	public String getPathParam(@PathParam("name") String name) {
		System.out.println(name);
		return "hello 蒋正午";
	}
	
	@GET  
	@Path("/getMultiPathParam/{names}")
	@Produces("text/plain;charset=UTF-8")
	public String getMultiPathParam(@PathParam("name") PathSegment cond) {
		System.out.println(cond.getMatrixParameters());
		return "hello 蒋正午";
	}
	
	
	
}
